package com.gmall.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig_DeadLetter {


    public static final String Exchange_Business_1 = "Exchange_Business_direct";
    public static final String Queue_Business_1 = "Queue_Business_1";
    public static final String Business_RoutingKey_1 = "Business_RoutingKey_1";
    public static final String Exchange_DeadLetter_1 = "Exchange_DeadLetter_direct";
    public static final String Queue_DeadLetter_1 = "Queue_DeadLetter_1";
    public static final String DeadLetter_RoutingKey_1 = "DeadLetter_RoutingKey_1";


    @Bean(Exchange_Business_1)
    public Exchange exchange_Business_1(){
        return ExchangeBuilder.directExchange(Exchange_Business_1).build();
    }

    @Bean(Queue_Business_1)
    public Queue queue_Business_1(){
        return QueueBuilder.durable(Queue_Business_1)
                .withArgument("x-dead-letter-exchange",Exchange_DeadLetter_1)
                .withArgument("x-dead-letter-routing-key",Business_RoutingKey_1)
                .withArgument("x-max-length", 1000)
                .deadLetterExchange(Exchange_DeadLetter_1)
                .deadLetterRoutingKey(DeadLetter_RoutingKey_1)
                .maxLength(1000)
                .build();
    }

    @Bean
    public Binding binding_business_1(
            @Qualifier(Queue_Business_1) Queue queue,
            @Qualifier(Exchange_Business_1) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(Business_RoutingKey_1).noargs();
    }

    @Bean(Exchange_DeadLetter_1)
    public Exchange Exchange_DeadLetter_1(){
        return ExchangeBuilder.directExchange(Exchange_DeadLetter_1).build();
    }

    @Bean(Queue_DeadLetter_1)
    public Queue Queue_DeadLetter_1(){
        return QueueBuilder.durable(Queue_DeadLetter_1).build();
    }


    @Bean
    public Binding binding_deadLetter_2(
            @Qualifier(Queue_DeadLetter_1) Queue queue,
            @Qualifier(Exchange_DeadLetter_1) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(DeadLetter_RoutingKey_1).noargs();
    }
}
